package org.softmed.rest.server.test.creators.testcases.handlers;

import org.restlet.data.Response;
import org.restlet.data.Status;
import org.softmed.rest.server.test.creators.AbstractTestCaseCreator;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

public class GetHandler extends AbstractTestCaseCreator {

	@Override
	public String testCase(String uri, String elementURI) throws Throwable {

		Response response = creator.get(elementURI);
		if (!response.getStatus().equals(Status.SUCCESS_OK))
			return elementURI;

		setupXMLDoc();

		if (!doc.getDocumentElement().getNodeName().equals("handler"))
			return elementURI;

		NamedNodeMap att = doc.getDocumentElement().getAttributes();

		if (att == null)
			return elementURI;

		Node node = att.getNamedItem("uri");
		if (node == null)
			return elementURI;

		String elementUri = null;
		elementUri = node.getNodeValue();
		if (elementUri == null)
			return elementURI;

		if (!elementUri.equals(elementURI))
			return elementURI;

		node = att.getNamedItem("active");
		if (node == null)
			return elementURI;

		String value = node.getNodeValue();
		if (value == null || !value.equals("false"))
			return elementURI;

		node = att.getNamedItem("path");
		if (node != null)
			return elementURI;

		creator.getTestCase().setSucess(true);

		return elementUri;
	}

}
